﻿Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Web.Script.Services
Imports sonicERPinfo
Imports sonicERPinterface
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="sonicERPinterface")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class WSAjax
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function HelloWorld() As String
        Return "Hello World"
    End Function

    <WebMethod()> _
    <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function AddTelefone(ByVal telvalor As String,
                                       ByVal count As Integer,
                                       ByVal teltext As String,
                                       ByVal tipotelefone As String
                                      ) As List(Of TelefoneInfo)
        Dim telefone = New TelefoneInfo()
        telefone.idvalue = telvalor + "," + count.ToString()
        Dim b As String = String.Empty
        b = String.Concat(teltext + "   ", tipotelefone)
        telefone.telefone = b
        Try
            Dim nome = CadastroCliente.lista.Find(Function(n) n.telefone = b)
            If TypeOf (nome) Is TelefoneInfo Then
                Throw New Exception("Ja Existe Esse telefone")
            End If
            CadastroCliente.lista.Add(telefone)
            CadastroCliente.ltbtel.DataSource = CadastroCliente.lista
            CadastroCliente.ltbtel.DataTextField = "telefone"
            CadastroCliente.ltbtel.DataValueField = "idvalue"
            CadastroCliente.ltbtel.DataBind()
            '  Dim data = JsonConvert.SerializeObject(lista, Formatting.None)
            Return CadastroCliente.lista

        Catch ex As Exception
            Throw New Exception(ex.Message)
        End Try

    End Function

    <WebMethod()> _
   <ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
    Public Function RemoverTel(ByVal lista As String
                                       ) As List(Of TelefoneInfo)
        Dim l As New List(Of TelefoneInfo)
        lista = lista.Substring(10)
        Dim list() As String = lista.Split(New Char() {"|"c})
        For Each item In list
            For Each it As TelefoneInfo In CadastroCliente.lista
                If item = it.idvalue Then
                    l.Add(it)
                End If
            Next
        Next
        For Each b As TelefoneInfo In l
            CadastroCliente.lista.Remove(b)
        Next
        CadastroCliente.ltbtel.DataSource = CadastroCliente.lista
        CadastroCliente.ltbtel.DataTextField = "telefone"
        CadastroCliente.ltbtel.DataValueField = "idvalue"
        CadastroCliente.ltbtel.DataBind()
        '  Dim data = JsonConvert.SerializeObject(lista, Formatting.None)

        Return CadastroCliente.lista
    End Function
End Class